package com.chuangjiangx.member.score.ddd.query;

import com.chuangjiangx.commons.page.PagingResult;
import com.chuangjiangx.member.basic.ddd.dal.dto.StoreList;
import com.chuangjiangx.member.score.dao.mapper.InMbrScoreGiftRecordMapper;
import com.chuangjiangx.member.score.dao.model.InMbrScoreGiftRecord;
import com.chuangjiangx.member.score.dao.model.InMbrScoreGiftRecordExample;
import com.chuangjiangx.member.score.ddd.dal.dto.MbrScoreGiftRecordDetail;
import com.chuangjiangx.member.score.ddd.dal.dto.ScoreGiftRecordList;
import com.chuangjiangx.member.score.ddd.dal.condition.QueryClaimCodeValidationHistoryCondition;
import com.chuangjiangx.member.coupon.ddd.dal.mapper.MbrCouponDalMapper;
import com.chuangjiangx.member.score.ddd.dal.mapper.MbrScoreGiftRecordDalMapper;
import com.chuangjiangx.member.score.ddd.dal.condition.QueryScoreGiftRecordCondition;

import com.chuangjiangx.member.basic.ddd.domain.model.MbrScoreGiftRecordId;
import com.chuangjiangx.member.score.ddd.query.dto.ClaimCodeValidationHistory;
import com.chuangjiangx.member.score.ddd.query.dto.ClaimCodeValidationHistoryDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Component
public class MbrScoreGiftRecordQuery {

    @Autowired
    private  MbrScoreGiftRecordDalMapper mbrScoreGiftRecordDalMapper;

    @Autowired
    private MbrCouponDalMapper mbrCouponDalMapper;

    @Autowired
    private InMbrScoreGiftRecordMapper inMbrScoreGiftRecordMapper;


    /**
     * 查询积分兑换记录列表(分页)
     */
    public PagingResult<ScoreGiftRecordList> findMbrScoreGiftRecordList(QueryScoreGiftRecordCondition condition) {
        Objects.requireNonNull(condition);
        int total = mbrScoreGiftRecordDalMapper.countMbrScoreGiftRecordList(condition);
        List<ScoreGiftRecordList> list = Collections.EMPTY_LIST;
        if(total > 0){
            list = mbrScoreGiftRecordDalMapper.findMbrScoreGiftRecordList(condition);
        }
        return new PagingResult<>(total,list);
    }



    /**
     * 获取会员积分兑换记录详情
     * @param recordId 记录id
     * @return 详情
     */
    public MbrScoreGiftRecordDetail getMbrScoreGiftRecordDetail(MbrScoreGiftRecordId recordId) {
        Objects.requireNonNull(recordId);
        MbrScoreGiftRecordDetail detail = mbrScoreGiftRecordDalMapper.getMbrScoreGiftRecordDetail(recordId.getId());
        if(null != detail){
            List<StoreList> storeLists = mbrCouponDalMapper.findStoreListByMerchantId(detail.getMerchantId(), null);
            detail.setStoreLists(storeLists);
        }
        return detail;
    }


    /**
     * 查询取货码核销历史列表
     * @param condition
     * @return
     */
    public List<ClaimCodeValidationHistory> findClaimCodeValidationHistoryList(QueryClaimCodeValidationHistoryCondition condition){
        return mbrScoreGiftRecordDalMapper.findClaimCodeValidationHistoryList(condition);
    }


    /**
     * 查询取货码核销详情
     * @param id
     * @return
     */
    public ClaimCodeValidationHistoryDetail findClaimCodeValidationHistoryDetail(Long id){
        return mbrScoreGiftRecordDalMapper.findClaimCodeValidationHistoryDetail(id);
    }

    public InMbrScoreGiftRecord findRecordDetailByScoreStreamId(Long scoreStreamId) {
        InMbrScoreGiftRecordExample example = new InMbrScoreGiftRecordExample();
        example.createCriteria().andMbrScoreStreamIdEqualTo(scoreStreamId);
        List<InMbrScoreGiftRecord> records = inMbrScoreGiftRecordMapper.selectByExample(example);
        if (!records.isEmpty()) {
            return records.get(0);
        }
        return null;
    }

}
